﻿//-------------------------------------------------------------------------------------------------------------------------------------
// Copyright (C) 2005 greatelsoft.com All Rights Reserved
//-------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Data.Common;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using GTS.Channel.Api.Entity;
using Pub.Class;

namespace GTS.Channel.Api.Helper {
    /// <summary>
    /// 操作类
    /// 
    /// 修改纪录
    ///     2012-04-11 版本：1.0 系统自动创建此类
    /// 
    /// </summary>
    public partial class ChannelDealDataHelper {
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="channelDealData">实体类</param>
        /// <param name="delCache">添加成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>添加是否成功</returns>
        public static bool Insert(ChannelDealData channelDealData, string[] delCache = null, string dbkey = "") {
            string insertSql = new SQL().Insert(ChannelDealData._)
                .Value(ChannelDealData._SPCode, channelDealData.SPCode)
                .Value(ChannelDealData._LinkID, channelDealData.LinkID)
                .Value(ChannelDealData._SMSKey, channelDealData.SMSKey)
                .Value(ChannelDealData._LongPhone, channelDealData.LongPhone)
                .Value(ChannelDealData._Mobile, channelDealData.Mobile)
                .Value(ChannelDealData._ReceiveQuery, channelDealData.ReceiveQuery)
                .Value(ChannelDealData._ResponseStatus, channelDealData.ResponseStatus)
                .Value(ChannelDealData._RequestDate, channelDealData.RequestDate)
                .Value(ChannelDealData._MountCode, channelDealData.MountCode)
                .Value(ChannelDealData._GameCode, channelDealData.GameCode)
                .Value(ChannelDealData._ChargeCode, channelDealData.ChargeCode)
                .Value(ChannelDealData._Price, channelDealData.Price)
                .Value(ChannelDealData._IsHidden, channelDealData.IsHidden)
                .Value(ChannelDealData._DeviceCode, channelDealData.DeviceCode)
                .Value(ChannelDealData._MRDate, channelDealData.MRDate)
                .Value(ChannelDealData._MODate, channelDealData.MODate)
                .Value(ChannelDealData._Telcoid, channelDealData.Telcoid)
                .Value(ChannelDealData._CountryCode, channelDealData.CountryCode)
                .Value(ChannelDealData._Unit, channelDealData.Unit)
                .Value(ChannelDealData._Area, channelDealData.Area)
                .Value(ChannelDealData._PLMN, channelDealData.PLMN)
                .Value(ChannelDealData._SMSContent, channelDealData.SMSContent)
                .Value(ChannelDealData._IMSI, channelDealData.IMSI)
                .ToString();
            string selectSql = new SQL().From(ChannelDealData._)
                .Select(ChannelDealData._LinkID)
                .Where("1=1").Where(
                    new Where()
                    .And(ChannelDealData._LinkID, channelDealData.LinkID, Operator.Equal)
                    .And(ChannelDealData._SPCode, channelDealData.SPCode, Operator.Equal)
                    .ToString()
                ).ToString();
            string strSql = "if not exists(" + selectSql + ") " + insertSql;
            bool istrue = dbkey.IsNullEmpty() ? Data.ExecSql(strSql) == 1 : Data.Pool(dbkey).ExecSql(strSql) == 1;
            if (delCache.IsNull()) return istrue;
            foreach (string s in delCache) {
                if (s.IndexOf("(") == -1 || s.IndexOf("[") == -1)
                    Cache2.Remove(s.IndexOf("Cache_") == -1 ? "ChannelDealDataCache_" + s : s);
                else
                    Cache2.RemoveByPattern(s.IndexOf("Cache_") == -1 ? "(ChannelDealDataCache_" + s + ")" : s);
            }
            return istrue;
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="channelDealData">实体类</param>
        /// <returns>添加是否成功</returns>
        public static bool Insert(ChannelDealData channelDealData) {
            return Insert(channelDealData, null, "");
        }
        /// <summary>
        /// 修改记录
        /// </summary>
        /// <param name="channelDealData">实体类</param>
        /// <param name="where">修改时附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="delCache">修改成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>修改是否成功</returns>
        public static bool Update(ChannelDealData channelDealData, string where = "", string[] delCache = null, string dbkey = "") {
            if (channelDealData.SPCode.IsNullEmpty()  && channelDealData.LinkID.IsNullEmpty()) return false;
            
            string insertSql = new SQL().Insert(ChannelDealData._)
                .Value(ChannelDealData._SPCode, channelDealData.SPCode)
                .Value(ChannelDealData._LinkID, channelDealData.LinkID)
                .Value(ChannelDealData._SMSKey, channelDealData.SMSKey)
                .Value(ChannelDealData._LongPhone, channelDealData.LongPhone)
                .Value(ChannelDealData._Mobile, channelDealData.Mobile)
                .Value(ChannelDealData._ReceiveQuery, channelDealData.ReceiveQuery)
                .Value(ChannelDealData._ResponseStatus, channelDealData.ResponseStatus)
                .Value(ChannelDealData._RequestDate, channelDealData.RequestDate)
                .Value(ChannelDealData._MountCode, channelDealData.MountCode)
                .Value(ChannelDealData._GameCode, channelDealData.GameCode)
                .Value(ChannelDealData._ChargeCode, channelDealData.ChargeCode)
                .Value(ChannelDealData._Price, channelDealData.Price)
                .Value(ChannelDealData._IsHidden, channelDealData.IsHidden)
                .Value(ChannelDealData._DeviceCode, channelDealData.DeviceCode)
                .Value(ChannelDealData._MRDate, channelDealData.MRDate)
                .Value(ChannelDealData._MODate, channelDealData.MODate)
                .Value(ChannelDealData._Telcoid, channelDealData.Telcoid)
                .Value(ChannelDealData._CountryCode, channelDealData.CountryCode)
                .Value(ChannelDealData._Unit, channelDealData.Unit)
                .Value(ChannelDealData._Area, channelDealData.Area)
                .Value(ChannelDealData._PLMN, channelDealData.PLMN)
                .Value(ChannelDealData._SMSContent, channelDealData.SMSContent)
                .Value(ChannelDealData._IMSI, channelDealData.IMSI)
                .ToString();
            
            string updateSql = new SQL().Update(ChannelDealData._)
                .Set(ChannelDealData._SMSKey, channelDealData.SMSKey)
                .Set(ChannelDealData._LongPhone, channelDealData.LongPhone)
                .Set(ChannelDealData._Mobile, channelDealData.Mobile)
                .Set(ChannelDealData._ReceiveQuery, channelDealData.ReceiveQuery)
                .Set(ChannelDealData._ResponseStatus, channelDealData.ResponseStatus)
                .Set(ChannelDealData._RequestDate, channelDealData.RequestDate)
                .Set(ChannelDealData._MountCode, channelDealData.MountCode)
                .Set(ChannelDealData._GameCode, channelDealData.GameCode)
                .Set(ChannelDealData._ChargeCode, channelDealData.ChargeCode)
                .Set(ChannelDealData._Price, channelDealData.Price)
                .Set(ChannelDealData._IsHidden, channelDealData.IsHidden)
                .Set(ChannelDealData._DeviceCode, channelDealData.DeviceCode)
                .Set(ChannelDealData._MRDate, channelDealData.MRDate)
                .Set(ChannelDealData._MODate, channelDealData.MODate)
                .Set(ChannelDealData._Telcoid, channelDealData.Telcoid)
                .Set(ChannelDealData._CountryCode, channelDealData.CountryCode)
                .Set(ChannelDealData._Unit, channelDealData.Unit)
                .Set(ChannelDealData._Area, channelDealData.Area)
                .Set(ChannelDealData._PLMN, channelDealData.PLMN)
                .Set(ChannelDealData._SMSContent, channelDealData.SMSContent)
                .Set(ChannelDealData._IMSI, channelDealData.IMSI)
                .Where("1=1").Where(new Where()
                    .And(ChannelDealData._SPCode, channelDealData.SPCode, Operator.Equal, true)
                    .And(ChannelDealData._LinkID, channelDealData.LinkID, Operator.Equal, true)
                ).ToString();

            string selectSql = new SQL().From(ChannelDealData._)
                .Select(ChannelDealData._LinkID)
                .Where("1=1").Where(
                    new Where()
                    .And(ChannelDealData._LinkID, channelDealData.LinkID, Operator.Equal)
                    .And(ChannelDealData._SPCode, channelDealData.SPCode, Operator.Equal)
                    .ToString()
                ).ToString();
            string strSql = "if not exists(" + selectSql + ") " + insertSql + " else " + updateSql;

            if (!where.IsNullEmpty()) strSql += where;
            int value = dbkey.IsNullEmpty() ? Data.ExecSql(strSql) : Data.Pool(dbkey).ExecSql(strSql);
            if (value <= 0) return false;
            if (delCache.IsNull()) return true;
            foreach (string s in delCache) {
                if (s.IndexOf("(") == -1 || s.IndexOf("[") == -1)
                    Cache2.Remove(s.IndexOf("Cache_") == -1 ? "ChannelDealDataCache_" + s : s);
                else
                    Cache2.RemoveByPattern(s.IndexOf("Cache_") == -1 ? "(ChannelDealDataCache_" + s + ")" : s);
            }
            return true;
        }
        /// <summary>
        /// 修改记录
        /// </summary>
        /// <param name="channelDealData">实体类</param>
        /// <returns>修改是否成功</returns>
        public static bool Update(ChannelDealData channelDealData) {
            return Update(channelDealData, "", null, "");
        }
    }
}

